﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace GEPSCodeTemplate
{
    public partial class ContentFrame : UserControl
    {
        private string _filename;
        public string Content
        {
            private set { txtContent.Text = value; }
            get { return txtContent.Text; }
        }

        private ContentFrame()
        {
            InitializeComponent();
        }

        public ContentFrame(string fileName, bool editMode = false)
            : this()
        {
            if (File.Exists(fileName))
            {
                _filename = fileName;
                FileStream fs = null;
                StreamReader sr = null;
                try
                {
                    fs = new FileStream(fileName, FileMode.Open);
                    sr = new StreamReader(fs);
                    Content = sr.ReadToEnd();
                }
                finally
                {
                    sr.Close();
                    fs.Close();
                }
                tbnSave.Visible = editMode;
            }
        }

        private void miSetParam_Click(object sender, EventArgs e)
        {
            txtContent.SelectedText = Param.GetParamString(Content, txtContent.SelectedText);
        }

        private void tbnCopyContent_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(Content);
        }

        private void miSetOptional_Click(object sender, EventArgs e)
        {
            txtContent.SelectedText = Param.GetOptionalString(txtContent.SelectedText);
        }

        private void tbnReplaceParam_Click(object sender, EventArgs e)
        {
            Content = ParamReplaceForm.Execute(Content);
        }

        private void tbnSave_Click(object sender, EventArgs e)
        {
            txtContent.SaveFile(_filename, RichTextBoxStreamType.PlainText);
        }
    }
}
